{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Reorder Data in Log Files"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #string #sorting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #字符串 #排序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: reorderLogFiles"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #重新排列日志文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个日志数组 <code>logs</code>。每条日志都是以空格分隔的字串，其第一个字为字母与数字混合的<em> </em><strong>标识符 </strong>。</p>\n",
    "\n",
    "<p>有两种不同类型的日志：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><strong>字母日志</strong>：除标识符之外，所有字均由小写字母组成</li>\n",
    "\t<li><strong>数字日志</strong>：除标识符之外，所有字均由数字组成</li>\n",
    "</ul>\n",
    "\n",
    "<p>请按下述规则将日志重新排序：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>所有 <strong>字母日志</strong> 都排在 <strong>数字日志</strong> 之前。</li>\n",
    "\t<li><strong>字母日志</strong> 在内容不同时，忽略标识符后，按内容字母顺序排序；在内容相同时，按标识符排序。</li>\n",
    "\t<li><strong>数字日志</strong> 应该保留原来的相对顺序。</li>\n",
    "</ul>\n",
    "\n",
    "<p>返回日志的最终顺序。</p>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>logs = [\"dig1 8 1 5 1\",\"let1 art can\",\"dig2 3 6\",\"let2 own kit dig\",\"let3 art zero\"]\n",
    "<strong>输出：</strong>[\"let1 art can\",\"let3 art zero\",\"let2 own kit dig\",\"dig1 8 1 5 1\",\"dig2 3 6\"]\n",
    "<strong>解释：</strong>\n",
    "字母日志的内容都不同，所以顺序为 \"art can\", \"art zero\", \"own kit dig\" 。\n",
    "数字日志保留原来的相对顺序 \"dig1 8 1 5 1\", \"dig2 3 6\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>logs = [\"a1 9 2 3 1\",\"g1 act car\",\"zo4 4 7\",\"ab1 off key dog\",\"a8 act zoo\"]\n",
    "<strong>输出：</strong>[\"g1 act car\",\"a8 act zoo\",\"ab1 off key dog\",\"a1 9 2 3 1\",\"zo4 4 7\"]\n",
    "</pre>\n",
    "\n",
    "<p> </p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 <= logs.length <= 100</code></li>\n",
    "\t<li><code>3 <= logs[i].length <= 100</code></li>\n",
    "\t<li><code>logs[i]</code> 中，字与字之间都用 <strong>单个</strong> 空格分隔</li>\n",
    "\t<li>题目数据保证 <code>logs[i]</code> 都有一个标识符，并且在标识符之后至少存在一个字</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [reorder-data-in-log-files](https://leetcode.cn/problems/reorder-data-in-log-files/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [reorder-data-in-log-files](https://leetcode.cn/problems/reorder-data-in-log-files/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"dig1 8 1 5 1\",\"let1 art can\",\"dig2 3 6\",\"let2 own kit dig\",\"let3 art zero\"]', '[\"a1 9 2 3 1\",\"g1 act car\",\"zo4 4 7\",\"ab1 off key dog\",\"a8 act zoo\"]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        def trans(log: str) -> tuple:\n",
    "            a, b = log.split(' ', 1)\n",
    "            return (b, a) if b[0].isalpha() else ('{',)\n",
    "\n",
    "        logs.sort(key=trans)  # sort 是稳定排序\n",
    "        return logs\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        def get_key(log):\n",
    "            _id, rest = log.split(\" \", 1)\n",
    "            return (0, rest, _id) if rest[0].isalpha() else (1,)\n",
    "\n",
    "        return sorted(logs, key=get_key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        def get_key(log):\n",
    "            _id, rest = log.split(\" \", 1)\n",
    "            return (0, rest, _id) if rest[0].isalpha() else (1,)\n",
    "\n",
    "        return sorted(logs, key=get_key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        alpha_logs=[]\n",
    "        num_logs=[]\n",
    "        for item in logs:\n",
    "            content=item.split(' ')[1]\n",
    "            if ord(content[0])>=ord('0') and ord(content[0])<=ord('9'):\n",
    "                num_logs.append(item)\n",
    "            else:\n",
    "                alpha_logs.append(item)\n",
    "\n",
    "        alpha_logs.sort(key=lambda x:[x[len(x.split(' ')[0]):], x.split(' ')[0]])\n",
    "        return alpha_logs+num_logs         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        def trans(log: str) -> tuple:\n",
    "            a, b = log.split(' ', 1)\n",
    "            return (0, b, a) if b[0].isalpha() else (1,)\n",
    "\n",
    "        logs.sort(key=trans)  # sort 是稳定排序\n",
    "        return logs\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\r\n",
    "        alpha, digit = list(), list()\r\n",
    "        for log in logs:\r\n",
    "            pre, cont = log.split(' ', 1)\r\n",
    "            if 'a' <= cont[0] <= 'z':\r\n",
    "                alpha.append(log)\r\n",
    "            elif '0' <= cont[0] <= '9':\r\n",
    "                digit.append(log)\r\n",
    "        alpha.sort(key=lambda x: (x.split(' ', 1)[1], x.split(' ', 1)[0]))\r\n",
    "        return alpha + digit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self,logs):\n",
    "        \"\"\"\n",
    "        :type logs: List[str]\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        l1=[]\n",
    "        l2=[]\n",
    "        for l in logs:\n",
    "            if l[-1].isalpha():\n",
    "                l1.append(l)\n",
    "            else:\n",
    "                l2.append(l)\n",
    "        l1.sort(key=lambda x:(x[x.index(' ')+1:],x[:x.index(' ')+1]))\n",
    "        return l1+l2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def is_num(self,alpht):\n",
    "        for i in alpht.split()[1:]:\n",
    "            if i.isdigit():\n",
    "                continue\n",
    "            else:\n",
    "                return False\n",
    "        return True\n",
    "    def reorderLogFiles(self, logs) :\n",
    "        digital = []\n",
    "        alpht = []\n",
    "        result = []\n",
    "        for i in logs:\n",
    "            if not self.is_num(i):\n",
    "                alpht.append(i)\n",
    "            else:\n",
    "                digital.append(i)\n",
    "        \n",
    "        # 对字符的排序\n",
    "        alpht_sort = []\n",
    "        for i,alphts in enumerate(alpht):\n",
    "            alphts = alphts.split()[1:]\n",
    "            a = ''\n",
    "            for j in alphts:\n",
    "                a+=j+' '\n",
    "            a = a[:-1]\n",
    "            alpht_sort.append([a,i])\n",
    "        alpht_sort = sorted(alpht_sort, key = lambda x:(x[0]))\n",
    "        for i in range(len(alpht_sort)-1):\n",
    "            if alpht_sort[i][0]==alpht_sort[i+1][0]:\n",
    "                b = [alpht[alpht_sort[i][1]],alpht[alpht_sort[i+1][1]]]\n",
    "                a = sorted([alpht[alpht_sort[i][1]],alpht[alpht_sort[i+1][1]]])\n",
    "                if  a==b:\n",
    "                    continue \n",
    "                else:\n",
    "                    temp = alpht_sort[i]\n",
    "                    alpht_sort[i] = alpht_sort[i+1]\n",
    "                    alpht_sort[i+1]= temp\n",
    "        # 结果\n",
    "        for i in alpht_sort:\n",
    "            result.append(alpht[i[1]])#+' '+i[0])\n",
    "        for i in digital:\n",
    "            result.append(i)\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        ls, ds = [], []\n",
    "        for log in logs:\n",
    "            for i, l in enumerate(log):\n",
    "                if l == ' ': break\n",
    "            part1 = log[: i]\n",
    "            part2 = log[i + 1:]\n",
    "            if part2[0].isdigit(): ds.append(log)\n",
    "            else: ls.append([part2, part1])\n",
    "        ls.sort()\n",
    "        ans = []\n",
    "        for p2, p1 in ls: ans.append(p1 + ' ' + p2)\n",
    "        for d in ds: ans.append(d)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        def get_key(log):\n",
    "            _id, rest = log.split(\" \", 1)\n",
    "            # print(_id)\n",
    "            # print(rest)\n",
    "            # print(0, rest, _id) if rest[0].isalpha() else (1,)\n",
    "            return (0, rest, _id) if rest[0].isalpha() else (1,)\n",
    "        return sorted(logs, key=get_key)\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        ls, ds = [], []\n",
    "        for log in logs:\n",
    "            for i, l in enumerate(log):\n",
    "                if l == ' ': break\n",
    "            part1 = log[: i]\n",
    "            part2 = log[i + 1:]\n",
    "            if part2[0].isdigit(): ds.append(log)\n",
    "            else: ls.append([part2, part1])\n",
    "        ls.sort()\n",
    "        ans = []\n",
    "        for p2, p1 in ls: ans.append(p1 + ' ' + p2)\n",
    "        for d in ds: ans.append(d)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        letters = []\n",
    "        nums = []\n",
    "        for log in logs:\n",
    "            logsplit = log.split(' ')\n",
    "            if logsplit[1].isalpha():\n",
    "                letters.append((' '.join(logsplit[1:]),logsplit[0]))\n",
    "            else:\n",
    "                nums.append(log)\n",
    "        letters.sort()\n",
    "        return [letter[1]+' '+letter[0] for letter in letters] + nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        def key_f(log):\n",
    "            if log[-1].isdigit():\n",
    "                return ['{']\n",
    "            return log.split(maxsplit=1)[::-1]\n",
    "        logs.sort(key = key_f)\n",
    "        return logs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        \n",
    "        dig_list = []\n",
    "        alpha_list = []\n",
    "        for item in logs:\n",
    "            temp = item.split(\" \")\n",
    "            if temp[1].isdigit():\n",
    "                dig_list.append(item)\n",
    "            else:\n",
    "                alpha_list.append(item)\n",
    "        mid = {}\n",
    "        ll = []\n",
    "        for item in alpha_list:\n",
    "            temp = item.split(\" \")\n",
    "            sub = \" \".join(temp[1:])\n",
    "            if sub not in mid:\n",
    "                mid[sub] = [item]\n",
    "                ll.append(sub)\n",
    "            else:\n",
    "                mid[sub].append(item)\n",
    "        ll.sort()\n",
    "        final = []\n",
    "        for i in range(len(ll)):\n",
    "            if len(mid[ll[i]]) != 1:\n",
    "                mid[ll[i]].sort()\n",
    "            for item in mid[ll[i]]:\n",
    "                final.append(item)\n",
    "        final += dig_list\n",
    "        return final"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        def trans(log: str) -> tuple:\n",
    "            a, b = log.split(' ', 1)\n",
    "            return (0, b, a) if b[0].isalpha() else (1,)\n",
    "\n",
    "        logs.sort(key=trans)  # sort 是稳定排序\n",
    "        return logs\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        def trans(log: str) -> tuple:\n",
    "            a, b = log.split(' ', 1)\n",
    "            return (0, b, a) if b[0].isalpha() else (1,)\n",
    "\n",
    "        logs.sort(key=trans)  # sort 是稳定排序\n",
    "        return logs\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "#         logs1,logs2=[],[]\n",
    "#         for item in range(len(logs)):\n",
    "#             if logs[item][-1].isalpha():\n",
    "#                 logs1.append(logs[item])\n",
    "#             else:\n",
    "#                 logs2.append(logs[item])\n",
    "#         logs1.sort(key=lambda x:x.split()[1:])\n",
    "#         return logs1+logs2\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        return sorted(logs, key=lambda log: (0, log.split(' ')[1:], log.split(' ')[0]) if log[-1].isalpha() else (1,))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        \n",
    "        letters = []\n",
    "        nums = []\n",
    "        for log in logs:\n",
    "            logsplit = log.split(' ')\n",
    "            if logsplit[1].isalpha():\n",
    "                letters.append((\" \".join(logsplit[1:]), logsplit[0]))\n",
    "            else:\n",
    "                nums.append(log)\n",
    "        \n",
    "        letters.sort()\n",
    "        return [letter[1] + ' ' + letter[0] for letter in letters] + nums\n",
    "    \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        chara = []\n",
    "        number = []\n",
    "        for log in logs:\n",
    "            l = log.split()\n",
    "            if 'a' <= l[1][0] <= 'z':\n",
    "                chara.append(log)\n",
    "            else:\n",
    "                number.append(log)\n",
    "        \n",
    "        def f(ch):\n",
    "            l = ch.split()\n",
    "            s = ' '.join(l[1:])\n",
    "            return (s, l[0])\n",
    "        \n",
    "        chara.sort(key=f)\n",
    "        return chara + number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        nums, letters = [], []\n",
    "        for log in logs:\n",
    "            log_split = log.split(\" \")\n",
    "            if log_split[1].isalpha():\n",
    "                letters.append((\" \".join(log_split[1:]), log_split[0]))\n",
    "            else:\n",
    "                nums.append(log)\n",
    "        letters.sort()\n",
    "        return [letter[1] + \" \" + letter[0] for letter in letters] + nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        def f(log):\n",
    "            id_, rest = log.split(' ',1)\n",
    "            return (0,rest, id_) if rest[0].isalpha() else (1,)\n",
    "        return sorted(logs, key = f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        def f(s):\n",
    "            id_, rest = s.split(' ', 1)\n",
    "            return (0, rest, id_) if rest[0].isalpha() else (1, )\n",
    "        return sorted(logs, key=f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        # 用一个函数实现：将字母/数字拆开做指令元组\n",
    "        def comparator(log):\n",
    "            identity, rest = log.split(\" \", 1)  # 按第一个出现的空格，分成两部分.1是分割一次的意思\n",
    "            if rest[0].isalpha():\n",
    "                return (0, rest, identity)      # 字母的排序\n",
    "            else:\n",
    "                return (1, '')                  # 数字的排序，放后面，且顺序按默认\n",
    "        return sorted(logs, key=comparator)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        def trans(log: str) -> tuple:\n",
    "            a, b = log.split(' ', 1)\n",
    "            return (0, b, a) if b[0].isalpha() else (1,)\n",
    "\n",
    "        logs.sort(key=trans)  # sort 是稳定排序\n",
    "        return logs\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        def get_key(n):\n",
    "            _id, rest = n.split(\" \", 1)\n",
    "            return (0, rest, _id) if rest[0].isalpha() else (1,)\n",
    "        return sorted(logs, key=get_key)\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        ans1 = []\n",
    "        ans2 = []\n",
    "        for log in logs:\n",
    "            if log[-1].isalpha():\n",
    "                ans1.append(log)\n",
    "            else:\n",
    "                ans2.append(log)\n",
    "        ans1.sort(key=lambda x:(x[x.index(' ')+1:],x[:x.index(' ')+1]))\n",
    "        return ans1 + ans2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs):\n",
    "        def trans(log: str) -> tuple:\n",
    "            a, b = log.split(' ', 1)\n",
    "            return (0, b, a) if b[0].isalpha() else (1,)\n",
    "\n",
    "        logs.sort(key=trans)  # sort 是稳定排序\n",
    "        return logs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        def key_f(log):\n",
    "            if log[-1].isdigit():\n",
    "                return ['{']\n",
    "            return log.split(maxsplit=1)[::-1]\n",
    "        logs.sort(key = key_f)\n",
    "        return logs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        lets = []\n",
    "        digs = []\n",
    "        for log in logs:\n",
    "            space = log.find(' ')\n",
    "            if log[space+1].isdigit():\n",
    "                digs.append(log)\n",
    "            else:\n",
    "                lets.append(log)\n",
    "        \n",
    "        lets.sort(key=lambda x: (x[x.find(' ')+1:], x[:x.find(' ')]))\n",
    "\n",
    "        return lets+digs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        zimu = []\n",
    "        shuzi = []\n",
    "\n",
    "        for idx, log in enumerate(logs):\n",
    "            lines = log.split(' ')\n",
    "\n",
    "            if lines[1].isdigit():\n",
    "                shuzi.append(log)\n",
    "            else:\n",
    "                zimu.append((\" \".join(lines[1:]), lines[0], idx))\n",
    "        zimu.sort(key=lambda x:(x[0],x[1]))\n",
    "\n",
    "        nzimu = []\n",
    "        for zz in zimu:\n",
    "            nzimu.append(logs[zz[2]])\n",
    "        # print(zimu)\n",
    "        # print(shuzi)\n",
    "        return (nzimu + shuzi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        slist = []\n",
    "        nlist = []\n",
    "        for s in logs:\n",
    "            sl = s.split()\n",
    "            if ord(sl[1][0])-ord('0') <= 9 and ord(sl[1][0])-ord('0')>=0:\n",
    "\n",
    "                nlist.append([sl[0],s[len(sl[0]):]])\n",
    "            else:\n",
    "                slist.append([sl[0],s[len(sl[0]):]])\n",
    "        slist.sort(key=lambda x:(x[1],x[0]))\n",
    "        res = []\n",
    "        for s in slist:\n",
    "            res.append(s[0]+s[1])\n",
    "        for s in nlist:\n",
    "            res.append(s[0]+s[1])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        alp_log = []\n",
    "        num_log = []\n",
    "\n",
    "        for item in logs:\n",
    "            lines = item.split(' ')\n",
    "            if ''.join(lines[1:]).isdigit():\n",
    "                num_log.append(item)\n",
    "            else:\n",
    "                alp_log.append({\n",
    "                    'cat': lines[0],\n",
    "                    'text': ' '.join(lines[1:]),\n",
    "                    'full': item\n",
    "                })\n",
    "\n",
    "        alp_log.sort(key=lambda x: (x['text'], x['cat']))\n",
    "\n",
    "        return [x['full'] for x in alp_log] + num_log\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self,logs):\n",
    "        \"\"\"\n",
    "        :type logs: List[str]\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        l1=[]\n",
    "        l2=[]\n",
    "        for l in logs:\n",
    "            if l[-1].isalpha():\n",
    "                l1.append(l)\n",
    "            else:\n",
    "                l2.append(l)\n",
    "        l1.sort(key=lambda x:(x[x.index(' ')+1:],x[:x.index(' ')+1]))\n",
    "        return l1+l2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        def get_key(log):\n",
    "            _id, rest = log.split(\" \", 1)\n",
    "            # print(_id)\n",
    "            # print(rest)\n",
    "            # print(0, rest, _id) if rest[0].isalpha() else (1,)\n",
    "            return (0, rest, _id) if rest[0].isalpha() else (1,)\n",
    "        return sorted(logs, key=get_key)\n",
    "\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self,logs):\n",
    "        \"\"\"\n",
    "        :type logs: List[str]\n",
    "        :rtype: List[str]\n",
    "        \"\"\"\n",
    "        l1=[]\n",
    "        l2=[]\n",
    "        for l in logs:\n",
    "            if l[-1].isalpha():\n",
    "                l1.append(l)\n",
    "            else:\n",
    "                l2.append(l)\n",
    "        l1.sort(key=lambda x:(x[x.index(' ')+1:],x[:x.index(' ')+1]))\n",
    "        return l1+l2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        func = lambda log: (0, log.split(' ')[1:], log.split(' ')[0]) if log[-1].isalpha() else (1, )\n",
    "        ret = sorted(logs, key=func)\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        def cmp_fn(x):\n",
    "            i, c = x.split(\" \", 1)\n",
    "            if c[0].isdigit():\n",
    "                return (1,)\n",
    "            return (0, c, i)\n",
    "        logs.sort(key=cmp_fn)\n",
    "        return logs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: List[str]) -> List[str]:\n",
    "        letter_logs, digit_logs = [], []\n",
    "        for log in logs:\n",
    "            if log.split()[1].isdigit():\n",
    "                digit_logs.append(log)\n",
    "            else:\n",
    "                letter_logs.append(log)\n",
    "        \n",
    "        letter_logs.sort(key= lambda x : (x.split()[1:], x.split()[0]))\n",
    "        return letter_logs + digit_logs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def reorderLogFiles(self, logs: list[str]) -> list[str]:\n",
    "        alphaLogs, numLogs=[],[]\n",
    "        for log in logs:\n",
    "            context = log[log.find(' ')+1:]\n",
    "            for i in range(0,10):\n",
    "                if str(i) in context:\n",
    "                    numLogs.append(log)\n",
    "                    break\n",
    "            else:\n",
    "                alphaLogs.append(log)\n",
    "        alphaLogs.sort()\n",
    "        alphaLogs.sort(key=lambda log:log[log.find(' ')+1:])\n",
    "        return alphaLogs+numLogs"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
